package com.hellogood.yaohuo.server.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.shared.data.CallDTO;
import com.hellogood.yaohuo.shared.entity.PsThDetail;
import com.hellogood.yaohuo.shared.entity.PsThForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;

public class PsThFormDAO extends FormDAO {
	Logger log = Logger.getLogger(this.getClass());

	public String createPeisongTuihuo(PsThForm form, boolean isModify) {
		String result = "";
		List<String> sqls = new ArrayList<String>();
		if (isModify) {
			String sql = "DELETE FROM PsThForm WHERE psThBillNo = '" + form.getPsThBillNo() + "'";
			sqls.add(sql);
			sql = "DELETE FROM PsThDetail WHERE psThBillNo = '" + form.getPsThBillNo() + "'";
			sqls.add(sql);
		}
		String sql = "INSERT INTO PsThForm(psThBillNo,psThDate,orgCode,vendorCode,vendorName,counts,cost,netcost,total,staffCode," + "staffName,rzDate,remark) VALUES('" + form.getPsThBillNo()
				+ "',CONVERT(varchar(100), GETDATE(), 23),'" + form.getOrgCode() + "','" + form.getVendorCode() + "','" + form.getVendorName() + "'," + form.getCounts() + "," + form.getCost() + ","
				+ form.getNetCost() + "," + form.getTotal() + ",'" + form.getStaffCode() + "','" + form.getStaffName() + "','" + form.getRzDate() + "','" + form.getRemark() + "')";
		sqls.add(sql);

		List<PsThDetail> list = form.getList();
		if (list != null && (!list.isEmpty())) {
			for (PsThDetail d : list) {
				sql = "INSERT INTO PsThDetail(psThBillNo,pluCode,pluName,barCode,depCode,depName,bra,color,size_s,size_m,size_l,size_xl,size_xxl,size_xxxl,size_avg,jPrice,price,"
						+ "counts,cost,netCost,total,rzDate,remark,orgCode,netJPrice) VALUES('"
						+ d.getBillNo()
						+ "','"
						+ d.getPluCode()
						+ "','"
						+ d.getPluName()
						+ "','"
						+ d.getBarCode()
						+ "','"
						+ d.getDepCode()
						+ "','"
						+ d.getDepName()
						+ "','"
						+ d.getBra()
						+ "','"
						+ d.getColor()
						+ "',"
						+ d.getSize_s()
						+ ","
						+ d.getSize_m()
						+ ","
						+ d.getSize_l()
						+ ","
						+ d.getSize_xl()
						+ ","
						+ d.getSize_xxl()
						+ ","
						+ d.getSize_xxxl()
						+ ","
						+ d.getSize_avg()
						+ ","
						+ d.getjPrice()
						+ ","
						+ d.getPrice()
						+ ","
						+ d.getCounts()
						+ ","
						+ d.getCost()
						+ ","
						+ d.getCost()
						+ ","
						+ d.getTotal()
						+ ",'"
						+ d.getRzDate()
						+ "','"
						+ d.getRemark() + "','" + d.getOrgCode() + "'," + d.getNetJPrice() + ")";
				sqls.add(sql);
			}
		}
		try {
			executeBatch(sqls);
		} catch (DatabaseException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return e.getMessage();
		}
		return result;
	}

	public String rz(String bill) throws DatabaseException {
		CallDTO dto = new CallDTO();
		String sql = "UPDATE psForm SET rzDate WHERE PsThBillNo = '" + bill + "'";
		String call = "{CALL psthrz(?,?)}";
//		String call = "{EXEC psthrz @ykbillNo = '" + bill + "',@sysdb='" + DBConnection.hxDbName + "'}";
		dto.setBill(bill);
		dto.setCall(call);
		dto.setSql(sql);
		dto.setHxdbName(DBConnection.hxDbName);
		return super.callProcedure(dto);
	}
}